#include "appointment.h"
#include "messagetype.h"
#include <QPalette>
#include <QFont>
#include<QMessageBox>
Appointment::Appointment(QTcpSocket *socket, QWidget *parent)
    : QWidget(parent), tcpSocket(socket) {

    // 设置窗口标题和大小
    setWindowTitle("挂号界面");
    setFixedSize(600, 700);  // 增大窗口尺寸，给控件提供更多的空间

    QVBoxLayout *layout = new QVBoxLayout(this);

    // 设置窗口背景颜色
    QPalette palette = this->palette();
    palette.setColor(QPalette::Window, QColor(240, 248, 255)); // 淡蓝色背景
    this->setPalette(palette);
    this->setAutoFillBackground(true);

    // 界面标题标签
    QLabel *label = new QLabel("挂号界面", this);
    label->setFont(QFont("微软雅黑", 20, QFont::Bold));
    label->setAlignment(Qt::AlignCenter);
    layout->addWidget(label);

    // 添加患者ID输入
    QLabel *patientIdLabel = new QLabel("患者ID:", this);
    patientIdLabel->setFont(QFont("微软雅黑", 16));
    layout->addWidget(patientIdLabel);
    patientIdEdit = new QLineEdit(this);
    patientIdEdit->setPlaceholderText("请输入患者ID");
    patientIdEdit->setFont(QFont("微软雅黑", 14));
    patientIdEdit->setFixedHeight(40);
    layout->addWidget(patientIdEdit);

    // 添加患者姓名输入
    QLabel *patientNameLabel = new QLabel("患者姓名:", this);
    patientNameLabel->setFont(QFont("微软雅黑", 16));
    layout->addWidget(patientNameLabel);
    patientNameEdit = new QLineEdit(this);
    patientNameEdit->setPlaceholderText("请输入患者姓名");
    patientNameEdit->setFont(QFont("微软雅黑", 14));
    patientNameEdit->setFixedHeight(40);
    layout->addWidget(patientNameEdit);

    // 添加科室输入
    QLabel *departmentLabel = new QLabel("科室:", this);
    departmentLabel->setFont(QFont("微软雅黑", 16));
    layout->addWidget(departmentLabel);
    departmentEdit = new QLineEdit(this);
    departmentEdit->setPlaceholderText("请输入科室");
    departmentEdit->setFont(QFont("微软雅黑", 14));
    departmentEdit->setFixedHeight(40);
    layout->addWidget(departmentEdit);

    // 添加医生姓名输入
    QLabel *doctorLabel = new QLabel("医生:", this);
    doctorLabel->setFont(QFont("微软雅黑", 16));
    layout->addWidget(doctorLabel);
    doctorEdit = new QLineEdit(this);
    doctorEdit->setPlaceholderText("请输入医生姓名");
    doctorEdit->setFont(QFont("微软雅黑", 14));
    doctorEdit->setFixedHeight(40);
    layout->addWidget(doctorEdit);

    // 添加预约日期输入
    QLabel *dateLabel = new QLabel("预约日期:", this);
    dateLabel->setFont(QFont("微软雅黑", 16));
    layout->addWidget(dateLabel);
    dateEdit = new QLineEdit(this);
    dateEdit->setPlaceholderText("请输入预约日期 (YYYY-MM-DD)");
    dateEdit->setFont(QFont("微软雅黑", 14));
    dateEdit->setFixedHeight(40);
    layout->addWidget(dateEdit);

    // 添加挂号按钮和返回按钮
    QPushButton *bookButton = new QPushButton("挂号", this);
    QPushButton *backButton = new QPushButton("返回", this);

    QFont buttonFont("微软雅黑", 16, QFont::Bold);
    QString buttonStyle = "QPushButton {"
                          "background-color: #4CAF50;"
                          "color: white;"
                          "border-radius: 8px;"
                          "padding: 12px;"
                          "} "
                          "QPushButton:hover {"
                          "background-color: #45A049;"
                          "}";

    bookButton->setFont(buttonFont);
    bookButton->setStyleSheet(buttonStyle);
    bookButton->setFixedHeight(50);  // 增大按钮的高度
    bookButton->setFixedWidth(150);  // 增大按钮的宽度，确保文字可以完整显示

    backButton->setFont(buttonFont);
    backButton->setStyleSheet(buttonStyle);
    backButton->setFixedHeight(50);
    backButton->setFixedWidth(150);

    // 按钮居中对齐
    QHBoxLayout *buttonLayout = new QHBoxLayout();
    buttonLayout->addStretch();
    buttonLayout->addWidget(bookButton);
    buttonLayout->addSpacing(20);
    buttonLayout->addWidget(backButton);
    buttonLayout->addStretch();

    layout->addLayout(buttonLayout);

    layout->setSpacing(20); // 增加控件之间的间距
    layout->setContentsMargins(30, 30, 30, 30); // 设置边距

    // 连接信号和槽函数
    connect(bookButton, &QPushButton::clicked, this, &Appointment::onBookAppointmentButtonClicked);
    connect(backButton, &QPushButton::clicked, this, &Appointment::onBackButtonClicked);
}

void Appointment::onBookAppointmentButtonClicked() {
    // 获取表单数据并发送到服务器
    QMap<QString, QString> appointmentData;
    appointmentData.insert("type", REGISTRATIONDATABASE);
    appointmentData.insert("databaseoperator", QString::number(INSERT));
    appointmentData.insert("patientid", patientIdEdit->text());
    appointmentData.insert("patientname", patientNameEdit->text());
    appointmentData.insert("departmentname", departmentEdit->text());
    appointmentData.insert("doctorname", doctorEdit->text());
    appointmentData.insert("date", dateEdit->text());

    QByteArray data = mapToByteArrayCustom(appointmentData);

    // 连接服务器并发送请求
    if (tcpSocket->state() == QAbstractSocket::UnconnectedState) {
        tcpSocket->connectToHost("127.0.0.1", 12345);
    }
    if (tcpSocket->waitForConnected(3000)) {
        tcpSocket->write(data);
        tcpSocket->flush();
        QMessageBox::information(this, "挂号成功", "您的挂号请求已提交成功，请耐心等待处理。");
    } else {
        QMessageBox::warning(this, "连接失败", "无法连接到服务器，请稍后再试。");
    }
}

void Appointment::onBackButtonClicked() {
    this->close();  // 关闭挂号窗口，返回上一级
}
